'use strict';

app.directive('onFinishRepeat', [ '$timeout', function($timeout) {
	return {
		restrict : 'A',
		link : function(scope, el, attrs) {
			if (scope.$last === true) {
				$timeout(function() {
					scope.$emit('ngRepeatFinished');
				})
			}
		}
	};
} ]);

//单位指令自动加上单位信息
app.directive('issUnit', [function () {
     return {
     	scope:{
     		value:'=',
     		type:'='
     	},
     	template:'<span class="number">{{value}}</span><span class="unit">{{unit}}</span>',
     	link: function(scope, el, attr) {
     		var value = scope.value;
     		var type = scope.type;
     		scope.unit = '';
	    	if(ISS.isNotEmpty(value)){
	    		var unit = '';
	    		if('voltage' == type){
	    			unit = 'V';
	    		}else if('current' == type){
	    			unit = 'A';
	    		}else if('frequency' == type){
	    			unit = 'Hz';
	    		}else if('power' == type){
	    			unit = 'kW';
	    		}else if('brightness' == type){
	    			unit = '';
	    		}else if('powerFactor' == type){
	    			unit = 'kVA';
	    		}else if('illumination' == type){
	    			unit = 'lm';
	    		}
				scope.unit = unit;
	    	}else{
	    		scope.value = '-';
	    	}
	    }
     }
}]);

/**
 * 左侧建筑选择指令
 * */
app.directive('locationSelected', ['$document', function($document) {
    return {
      restrict: 'AC',
      link: function(scope, el, attr) {
      		var target = attr.locationSelected;
      		var selectedEle = attr.selected;
      		el.on('click', function(){
      			if(angular.element( event.target ).closest('.area-caret').length == 0){
		      		angular.element('.data-summary i.icon-eye').removeClass("show");
		      		angular.element('.data-summary .selected').removeClass('selected');
		      		if(angular.equals(selectedEle, 'this')){
		      			angular.element(this).toggleClass('selected');
		      		}else{
						angular.element(selectedEle).toggleClass('selected');
		      		}
					if(target){
						angular.element(target).each(function(){
							$(this).find('i.icon-eye').toggleClass('show');
						});
					}
      			}
      		})
      }
    };
}]);


